﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nwrole.toolbox.sync.entity;
using nwrole.toolbox.sync;
using nwrole.toolbox.sync.contract;

namespace sync_test.action
{
    public class ChananSync {

        public static void Run(List<SyncEntity> maps, DateTimeTableSync sync, ISyncDestinationService client)
        {
            int counter = 0;
            foreach (var map in maps)
            {
                counter++;
                Console.Write("Start Sync " + map.Name + " ....");
                var result = syncTable(1000, map, sync, client);
                Console.WriteLine(" count:" + result.RowCount + " Done.");
            }
        }

        private static SyncResult syncTable(int batchSize, SyncEntity map, DateTimeTableSync sync, ISyncDestinationService client)
        {
            try
            {
                // setup sync
                // var client = InitializeWS<ITestService>();
                // GhostTableSync sync = GetGhostTableSync(batchSize);
                // run the sync

                //sync.CreateGhostInt(map, true);
                Guid guid = Guid.NewGuid();
                var result = sync.SyncWithReaderAndDirectUpdate(map, (bytes, tableName, schema) =>
                {
                    Console.Write(".");
                    SyncMessageEnvelope msg = new SyncMessageEnvelope(tableName, "ibbe3", bytes, schema, guid);
                    client.ProcessMessageOld(msg);
                    return 1;
                });

                map.SyncDat = DateTime.Now;

               // SyncEntity.Update(map, SyncSettings.SrcConnectionString);

                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return null;
        }
    }
}
